/**
 ******************************************************************************
 * @file      AFNf1.c
 * @brief     C Source file of AFNf1.c.
 * @details   This file including all API functions's implement of AFNf1.c.
 * @copyright
 ******************************************************************************
 */

/*-----------------------------------------------------------------------------
 Section: Includes
 ----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>

#include "../GDW1376_2.h"
#include "lib.h"
#include "maths.h"
#include "types.h"

#pragma pack(1)
typedef struct {
  uint8_t type;  /* 规约类型 */
  uint8_t defno; /* 保留 */
  uint16_t dLen; /* 数据长度 */
} curInfo_t;

typedef struct {
  uint8_t type;  /* 规约类型 */
  uint16_t dLen; /* 数据长度 */
} upCurrInfo_t;

#pragma pack()

/**
 ******************************************************************************
 * @brief   输出并发抄表解析字符串
 * @param[in]   dir         : 传输方向:1上行(模块->集中器)" 0下行(集中器->模块)
 * @param[in]  *pin         : 输入报文
 * @param[in]   len         : 应用层报文长度
 * @param[in]  *pcb         : 回调函数
 * @param[in]  *pline_head  : 每行起始填充字符串
 * @param[in]  *pline_end   : 每行结束填充字符串
 *
 * @return  0 : 解析成功
 * @return -1 : 解析失败
 ******************************************************************************
 */
static int AFNF1_FN01(unsigned char dir, const unsigned char *pin, int len,
                      pcallback pcb, const char *pline_head,
                      const char *pline_end) {
  unsed(dir);
  unsed(len);
  unsed(pin);
  uint8_t type;
  uint16_t sLen;
  curInfo_t *info = NULL;

  uint8_t *pindex = NULL;
  uint8_t *buffIndex = NULL;
  upCurrInfo_t *upInfo = NULL;
  uint16_t dLen;
  pcb(pline_head);
  pcb("并发抄表");
  pcb(pline_end);
  if (dir == 0)  //下行
  {
    info = (curInfo_t *)pin;
    type = info->type;
    sLen = info->dLen;
    info++;
    pindex = (uint8_t *)info;
    sprintf(buf, "%s 报文方向:下行 %s", pline_head, pline_end);
  } else  //上行
  {
    //无
    upInfo = (upCurrInfo_t *)pin;
    type = upInfo->type;
    sLen = upInfo->dLen;
    upInfo++;
    pindex = (uint8_t *)upInfo;
    sprintf(buf, "%s 报文方向: 上行 %s", pline_head, pline_end);
  }
  pcb(buf);

  switch (type) {
    case 0:
      sprintf(buf, "%s 规约类型: 透明传输 报文长度:%d %s", pline_head,
              info->dLen, pline_end);
      break;
    case 1:
      sprintf(buf, "%s 规约类型: DL/T 645-1997 报文长度:%d %s", pline_head,
              info->dLen, pline_end);
      break;
    case 2:
      sprintf(buf, "%s 规约类型: DL/T 645-2007 报文长度:%d %s", pline_head,
              info->dLen, pline_end);
      break;
    case 3:
      sprintf(buf, "%s 规约类型: DL/T698.45 报文长度:%d %s", pline_head,
              info->dLen, pline_end);
      break;
    default:
      sprintf(buf, "%s 规约类型: 未知类型 报文长度:%d %s", pline_head,
              info->dLen, pline_end);
      break;
  }
  pcb(buf);

  sprintf(buf, "%s 传输数据: ", pline_head);
  buffIndex = (uint8_t *)(buf + 16);
  for (dLen = 0; dLen < sLen; dLen++) {
    sprintf((char *)buffIndex, "%02x ", *pindex);
    buffIndex+= 2;
    pindex++;
  }
  sprintf((char *)buffIndex, "%s: ", pline_end);
  pcb(buf);

  return 0;
}

/**
 ******************************************************************************
 * @brief   输出内部测试解析字符串
 * @param[in]   dir         : 传输方向:1上行(模块->集中器)" 0下行(集中器->模块)
 * @param[in]  *pin         : 输入报文
 * @param[in]   len         : 应用层报文长度
 * @param[in]  *pcb         : 回调函数
 * @param[in]  *pline_head  : 每行起始填充字符串
 * @param[in]  *pline_end   : 每行结束填充字符串
 *
 * @return  0 : 解析成功
 * @return -1 : 解析失败
 ******************************************************************************
 */
int print_AFNf1(unsigned char dir, const unsigned char *pin, int len,
                pcallback pcb, const char *pline_head, const char *pline_end) {
  unsigned char DT1 = pin[0];
  unsigned char DT2 = pin[1];
  int fn = get_clear_fn(&DT1, &DT2);
  unsed(len);
  unsed(dir);
  switch (fn) {
    case 1:  //并发抄表
      return AFNF1_FN01(dir, pin + 2, len - 2, pcb, pline_head, pline_end);

    default:
      break;
  }

  pcb(pline_head);
  pcb("ERROR:不支持的FN!");
  pcb(pline_end);

  return -ERR_AFN_FN;
}

/*--------------------------------AFNf1.c------------------------------------*/
